<?php

namespace app\admin\controller\ykjp\summary;

use app\common\controller\Backend;
use think\Db;
use app\admin\model\ykjp\product\Product;
use app\admin\model\ykjp\information\basisinfo\Supplier;
use app\admin\model\ykjp\information\basisinfo\Warehouse;
use app\admin\model\ykjp\information\basisinfo\Partition;

/**
 * 采购汇总
 *
 * @icon fa fa-circle-o
 */
class Purchase extends Backend {

    protected $model = null;
    protected $searchFields = "name";
    protected $distinguish = true;
    protected $wherename = "";

    public function _initialize() {
        parent::_initialize();
        $this->model = new \app\admin\model\ykjp\purchase\Stoproduct;
    }

    /**
     * 查看
     *
     */
    public function index() {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $this->wherename = "stoproduct.firmid";
            // var_dump(11);exit;
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->with(['storage'])
                    ->where("stoproduct.status", "3")
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->with(['storage'])
                    ->where($where)
                    ->where("stoproduct.status", "3")
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            // var_dump($list);exit;
            $prodcutModel = new Product;
            $supplierModel = new Supplier;
            $WarehouseModel = new Warehouse;
            $PartitionModel = new Partition;
            $totalNums = 0;
            $totalMoney = 0;
            foreach ($list as $row) {
                // var_dump($row);exit;
                $row['productName'] = "";
                $row['supplier'] = "";
                $row['warehouse'] = "";
                $row['partition'] = "";
                $productName = $prodcutModel->where("firmid", $this->auth->firmid)->where("id", $row['product_id'])->Field("name")->find();
                if ($productName) {
                    $row['productName'] = $productName["name"];
                }

                $supplierName = $supplierModel->where("firmid", $this->auth->firmid)->where("id", $row['storage']['supplier_id'])->Field("name")->find();
                if ($supplierName) {
                    $row['supplier'] = $supplierName["name"];
                }

                $wareName = $WarehouseModel->where("firmid", $this->auth->firmid)->where("id", $row['warehouse_id'])->Field("name")->find();
                if ($wareName) {
                    $row['warehouse'] = $wareName["name"];
                }
                $PartName = $PartitionModel->where("firmid", $this->auth->firmid)->where("id", $row['partition_id'])->Field("name")->find();
                if ($PartName) {
                    $row['partition'] = $PartName["name"];
                }
                $totalNums += $row['nums'];
                $totalMoney += $row['subtotal'];
                $row->getRelation('storage')->visible(['id', "code", "inbound_type"]);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list, "extend" => array("totalNums" => $totalNums, "totalMoney" => $totalMoney));

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 供应商信息
     *
     */
    public function supplier() {
        $this->model = new Supplier;
        return parent::selectpage();
    }

    /**
     * 产品信息
     *
     */
    public function product() {
        $this->model = new Product;
        return parent::selectpage();
    }

    /**
     * 仓库信息
     *
     */
    public function getWarehose($type = null) {
        if ($this->request->isAjax()) {
            $Warehouse = new Warehouse;
            $Partition = new Partition;
            if ($type == "ware") {
                $list = $Warehouse->where("firmid", $this->auth->firmid)->Field('id as value, name')->select();
                $this->success(null, null, $list);
            } else {
                $list = $Partition->where("firmid", $this->auth->firmid)->field('id as value, name')->select();
                $this->success(null, null, $list);
            }
        }
        $this->error(__("Illegal operation"));
    }

}
